package com.mzy.linear_struct.hot100.动态规划;

/**
 * @Author: mizy3
 * @Date: 2024/09/07/下午3:01
 * @Description:
 */
public class Pro53最大子数组和 {
    private int maxSubArray(int[] nums) {
        if (nums.length == 1) return nums[0];

        int[] dp = new int[nums.length];
        dp[0] = nums[0];


        int ans = nums[0];
        for (int i = 1; i < dp.length; i++) {
            dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
            ans = Math.max(ans, dp[i]);
        }

        return ans;
    }
    /*
    public int maxSubArray(int[] nums) {

        // -2,1,-3,4,-1,2,1,-5,4
        //
        int ans = Integer.MIN_VALUE;
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            max += nums[i];
            ans = Math.max(ans, max);
            if (max < 0) {
                max = 0;
            }
        }

        return ans;
    }
    */

/*
    public int maxSubArray(int[] nums) {
        // dp[i]表示i之前最大连续子数组和
        int[] dp = new int[nums.length];
        // -2,1,-3,4,-1,2,1,-5,4
        // dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);

        dp[0] = nums[0];
        int ans = nums[0];
        for (int i = 1; i < nums.length; i++) {
            dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
            ans = Math.max(ans, dp[i]);
        }
        return ans;

    }*/

    public static void main(String[] args) {
        System.out.println(new Pro53最大子数组和().maxSubArray(new int[]{5, 4, -1, 7, 8}));
    }


}
